#include <functional>
#include <iostream>
#include <map>

/*
以下代码演示 std::function 绑定函数对象 实现一个 书籍管理系统
*/
void doShowAllBooks() {
    std::cout << "查看所有书籍信息" << std::endl;
}
void doBorrow() {
    std::cout << "借书" << std::endl;
}
void doReturn() {
    std::cout << "还书" << std::endl;
}
void doQuery() {
    std::cout << "查询书籍" << std::endl;
}
void doLogout() {
    std::cout << "注销" << std::endl;
}

int main() {
    std::map<int, std::function<void()>> actionMap;
    actionMap[1] = doShowAllBooks;
    actionMap[2] = doBorrow;
    actionMap[3] = doReturn;
    actionMap[4] = doQuery;
    actionMap[5] = doLogout;
    int choice = 0;
    for (;;) {
        std::cout << "--------------------" << std::endl;
        std::cout << "1. 查看所有书籍信息" << std::endl;
        std::cout << "2. 借书" << std::endl;
        std::cout << "3. 还书" << std::endl;
        std::cout << "4. 查询书籍" << std::endl;
        std::cout << "5. 注销" << std::endl;
        std::cout << "--------------------" << std::endl;
        std::cout << "请选择：";
        std::cin >> choice;

        auto it = actionMap.find(choice);
        if (it != actionMap.end()) {
            // find key
            it->second();
        } else {
            std::cout << "输入错误，请重新输入！" << std::endl;
        }
    }
    return 0;
}
